<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="跟进名称" prop="followName">
        <el-input
          v-model="form.followName"
          clearable
          placeholder="请输入跟进名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="合同" prop="contractId">
        <el-select
          v-model="form.contractId"
          placeholder="请选择合同"
          style="width: 100%"
          remote
          filterable
          remote-show-suffix
          :remote-method="queryContract"
          :loading="selectContractLoading"
          clearable
        >
          <el-option
            v-for="item in contractList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跟进类型" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择跟进类型"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in followTypeOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跟进内容" prop="content" style="width: 100%">
        <el-input
          v-model="form.content"
          clearable
          type="textarea"
          rows="3"
          placeholder="请输入跟进内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="跟进进度" prop="progress">
        <el-input
          v-model="form.progress"
          clearable
          placeholder="请输入跟进进度"
        ></el-input>
      </el-form-item>
      <el-form-item label="下次联系时间" prop="nextTime">
        <el-date-picker
          v-model="form.nextTime"
          type="datetime"
          placeholder="请选择日期和时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>

    <!-- 审核流程项  2023.7.24 跟进不需要审核-->
    <!-- <check-form-item
      ref="checkFormItem"
      @setCheckConfig="setCheckConfig"
      :field="this.form"
      :checkType="checkType"
      v-if="mode == 'add'"
    ></check-form-item> -->

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import { queryTypeByDic } from '@/utils/getData'
import checkFormItem from '@/components/checkProcess/checkFormItem.vue'
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      checkType: 7,//工作流的类型
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      isCheckLoaing: false,
      selectContractLoading: false,
      selectLoading: false,
      //表单数据
      form: {
        adminId: '',//用户登录时的id
        departmentId: '',
        contractId: '',
        // projectName: "",
        followName: '',//跟进名称
        content: '',//跟进内容
        progress: '',//跟进进度
        type: '',//跟进类型
        nextTime: '',//下次联系时间
        workflowJsonList: []
      },
      contractList: [],//合同列表
      followTypeOptions: [],
      //验证规则
      rules: {
        followName: [
          { required: true, message: '请输入跟进记录名称' }
        ],
        contractId: [
          { required: true, message: '请选择合同', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入跟进内容' }
        ],
        progress: [
          { required: true, message: '请输入跟进进度' }
        ],
        type: [
          { required: true, message: '请选择跟进类型', trigger: 'change' }
        ],
        nextTime: [
          { required: true, message: '请选择下次拜访时间', trigger: 'change' }
        ],

        userId: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ],
      }
    }
  },
  components: {
    checkFormItem
  },
  mounted () {
    this.getContractList()
    this.getType()
  },
  methods: {
    // 获取跟进类型
    async getType () {
      this.followTypeOptions = await queryTypeByDic('followType')
    },
    // 获取合同列表
    async getContractList (val) {
      this.selectContractLoading = true
      let param = {
        limit: 1000,
        page: 1,
        search: val
      }
      let res = await this.$API.contract.getContracts.get(param)
      this.selectContractLoading = false
      this.contractList = res.data
    },
    // 搜索合同
    queryContract (val) {
      this.getContractList(val)
    },


    // 审批流配置
    setCheckConfig (list) {
      this.form.workflowJsonList = list
    },
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.departmentId = userInfo.departmentId
      this.form.adminId = userInfo.userId
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          if (this.mode == 'add') {
            console.log(this.form)
            res = await this.$API.contract.followRecord.addFollowRecord.post(this.form)
          } else if (this.mode == 'edit') {
            delete this.form.auditType
            delete this.form.submitAudit
            delete this.form.createTime
            delete this.form.updateTime
            delete this.form.cause
            res = await this.$API.contract.followRecord.updateFollowRecord.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },
    //表单注入数据
    setData (data) {
      this.form = data
      this.form.type = Number(this.form.type)
    }
  }
}
</script>

<style>
</style>
